using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponTargetDetector : MonoBehaviour
{
	public static WeaponTargetDetector instance;

	public static HashSet<Mortal> otherPlayers = new HashSet<Mortal>();

	[Header("Ширина зоны захвата")]
	public float maxAimZoneWidth = 300f;

	public float minAimZoneWidth = 100f;

	public bool fixedMaxZoneWidth;

	[Header("Диапазон расстояний для изменения ширины зоны захвата")]
	public float maxDistanceAimZone = 300f;

	public float minDistanceAimZone = 10f;

	public Mortal nearestEnemy;

	public bool homingAim;

	public float maxDistance = 1000f;

	[Range(1f, 10f)]
	public float coef = 1f;

	public bool enemyCatchedForHomingMissle;

	private float _factorTimeofGoalCapture = 0.5f;

	private Coroutine watchEnemyIE;

	public LayerMask layerMask;

	private RaycastHit cameraRayHit;

	private Vector3 aimHitPoint;

	public float homingMissleFlyHeight = 20f;

	public Vector3 highestMisslePosition;

	private void Awake()
	{
		instance = this;
	}

	public static void InsertToTargetList(Mortal target)
	{
		if (!(target is Humanoid))
		{
			otherPlayers.Add(target);
		}
	}

	public static void RemoveFromTargetList(Mortal target)
	{
		otherPlayers.Remove(target);
	}

	public void StartWatchEnemyPlayers()
	{
		if (watchEnemyIE == null)
		{
			watchEnemyIE = CoroutineRunner.Instance.StartCoroutine(WatchEnemyPlayersIE());
		}
	}

	public void StopWatchEnemyPlayers()
	{
		if (watchEnemyIE != null)
		{
			CoroutineRunner.Instance.StopCoroutine(watchEnemyIE);
			watchEnemyIE = null;
		}
		if (TargetDetectorUI.instance != null)
		{
			TargetDetectorUI.instance.DisableAllInterface();
		}
	}

	private IEnumerator WatchEnemyPlayersIE()
	{
		if (TargetDetectorUI.instance == null)
		{
			yield break;
		}
		nearestEnemy = null;
		Mortal lastEnemy = nearestEnemy;
		while (true)
		{
			yield return null;
			if (otherPlayers.Count == 0 || PlayerBehavior.MyPlayer == null)
			{
				TargetDetectorUI.instance.DisableAllInterface();
				continue;
			}
			float dist;
			nearestEnemy = GetNearestEnemy(lastEnemy, out dist);
			TargetDetectorUI.instance.UpdateFocusOnTarget(nearestEnemy, dist, nearestEnemy != lastEnemy);
			lastEnemy = nearestEnemy;
		}
	}

	private Mortal GetNearestEnemy(Mortal lastEnemy, out float distance)
	{
		Mortal mortal = null;
		float num = 360f;
		foreach (Mortal otherPlayer in otherPlayers)
		{
			if (otherPlayer == null || !otherPlayer.enabled || otherPlayer.isDead || (!settings.offlineMode && PhotonNetwork.connected && PlayerBehavior.IsTeammate(GameController.instance.FindPlayer(otherPlayer.photonView.ownerId))) || (otherPlayer is PlayerBehavior && (otherPlayer as PlayerBehavior == PlayerBehavior.MyPlayer || (otherPlayer as PlayerBehavior).inCar || (otherPlayer as PlayerBehavior).inHelic)) || (otherPlayer is EntityBehavior && otherPlayer as EntityBehavior == PlayerBehavior.MyPlayer.currentCar))
			{
				continue;
			}
			Vector3 vector = new Vector3(otherPlayer.transform.position.x, otherPlayer.transform.position.y + 0.5f, otherPlayer.transform.position.z);
			if (TargetDetectorUI.instance.TargetIsOnScreen(vector))
			{
				Vector3 vector2 = vector - TPSCamera.instance.transform.position;
				Vector3 forward = TPSCamera.instance.transform.forward;
				float num2 = Vector3.Angle(vector2, forward);
				RaycastHit hitInfo;
				if (Physics.Raycast(TPSCamera.instance.transform.position, vector2, out hitInfo, 1000f, layerMask) && hitInfo.collider.GetComponentInParent<Mortal>() == otherPlayer && num2 < num)
				{
					num = num2;
					mortal = otherPlayer;
				}
			}
		}
		distance = 0f;
		if (mortal != null && !mortal.Equals(lastEnemy))
		{
			SetDelayAimValues(mortal);
		}
		return mortal;
	}

	private void SetDelayAimValues(Mortal nearestTarget)
	{
		Vector3 positionForChargeHit = nearestTarget.GetPositionForChargeHit();
		Vector3 direction = positionForChargeHit - PlayerBehavior.MyPlayer.transform.position;
		float num = 1f;
		RaycastHit hitInfo;
		if (Physics.Raycast(PlayerBehavior.MyPlayer.transform.position, direction, out hitInfo, 1000f, PlayerBehavior.MyPlayer.collisionLayer) && hitInfo.collider.gameObject.layer != LayerMask.NameToLayer("Car") && hitInfo.collider.gameObject.layer != LayerMask.NameToLayer("PlayerLayer"))
		{
			num += _factorTimeofGoalCapture;
		}
		TargetDetectorUI.instance.CatchAlpha.offset = TargetDetectorUI.instance.DefaultCatchAlpahOffset * num;
		TargetDetectorUI.instance.FocusScale.time = TargetDetectorUI.instance.DefaultFocusScaleTime * num;
	}

	public float GetAimZoneWidthForTarget(float distanceToTarget)
	{
		float result = Screen.width;
		if (maxAimZoneWidth > 0f && minAimZoneWidth > 0f)
		{
			float num = Mathf.Clamp(distanceToTarget, minDistanceAimZone, maxDistanceAimZone);
			float num2 = (num - minDistanceAimZone) / (maxDistanceAimZone - minDistanceAimZone);
			result = num2 * (maxAimZoneWidth - minAimZoneWidth) + minAimZoneWidth;
		}
		return result;
	}
}
